package com.yzt.dynamicmodule.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.yzt.zhuju.photoview.PhotoViewManager;
import com.yzt.zhuju.widget.tuiview.TUIFRelativeLayout;
import com.yzt.zhuju.widget.tuiview.TUIFTextView;
import com.yzt.dynamicmodule.R;
import com.yzt.p_base.AppUtils;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.plugin_widget.RoundFImageView;

import java.util.List;

/**
 * author : bob
 * date   : 2023/2/27
 * desc   : 动态模块的图片加载
 */
@SuppressLint("SetTextI18n")
public class DynamicImageView extends LinearLayout {

    private final Context mContext;

    private List<String> imagesList;

    private RoundFImageView img1, img2, img3;

    private TUIFTextView tv_all_num;

    private TUIFRelativeLayout rl_more;

    public DynamicImageView(Context context) {
        super(context);
        this.mContext = context;
    }

    public DynamicImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        initView();
    }

    private void initView() {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.layout_dynamic_imgview, this, true);
        img1 = mView.findViewById(R.id.img_double_left);
        img2 = mView.findViewById(R.id.img_double_center);
        img3 = mView.findViewById(R.id.img_double_right);
        tv_all_num = mView.findViewById(R.id.tv_all_num);
        rl_more = mView.findViewById(R.id.rl_more);

        img1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                PhotoViewManager.getInstance(AppUtils.getApp()).showPhotoView(view, imagesList, 0);
            }
        });

        img2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                PhotoViewManager.getInstance(AppUtils.getApp()).showPhotoView(view, imagesList, 1);
            }
        });

        img3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                PhotoViewManager.getInstance(AppUtils.getApp()).showPhotoView(view, imagesList, 2);
            }
        });
    }

    public void setList(List<String> lists, int type) throws IllegalArgumentException {
        if (lists == null) {
            throw new IllegalArgumentException("imageList is null...");
        }
        imagesList = lists;

        rl_more.setSelected(false);

        if (lists.size() == 1) {
            img1.setVisibility(View.VISIBLE);
            img2.setVisibility(View.INVISIBLE);
            img3.setVisibility(View.INVISIBLE);
            tv_all_num.setVisibility(INVISIBLE);
            ImageLoadUtil.load(mContext, lists.get(0), img1, R.mipmap.icon_placeholder);
        } else if (lists.size() == 2) {
            img1.setVisibility(View.VISIBLE);
            img2.setVisibility(View.VISIBLE);
            img3.setVisibility(View.INVISIBLE);
            tv_all_num.setVisibility(INVISIBLE);
            ImageLoadUtil.load(mContext, lists.get(0), img1, R.mipmap.icon_placeholder);
            ImageLoadUtil.load(mContext, lists.get(1), img2, R.mipmap.icon_placeholder);
        } else if (lists.size() == 3) {
            img1.setVisibility(View.VISIBLE);
            img2.setVisibility(View.VISIBLE);
            img3.setVisibility(View.VISIBLE);
            tv_all_num.setVisibility(INVISIBLE);
            ImageLoadUtil.load(mContext, lists.get(0), img1, R.mipmap.icon_placeholder);
            ImageLoadUtil.load(mContext, lists.get(1), img2, R.mipmap.icon_placeholder);
            ImageLoadUtil.load(mContext, lists.get(2), img3, R.mipmap.icon_placeholder);
        } else {
            img1.setVisibility(View.VISIBLE);
            img2.setVisibility(View.VISIBLE);
            img3.setVisibility(View.VISIBLE);
            tv_all_num.setVisibility(VISIBLE);
            ImageLoadUtil.load(mContext, lists.get(0), img1, R.mipmap.icon_placeholder);
            ImageLoadUtil.load(mContext, lists.get(1), img2, R.mipmap.icon_placeholder);
            ImageLoadUtil.load(mContext, lists.get(2), img3, R.mipmap.icon_placeholder);
            tv_all_num.setText("+" + (lists.size() - 3));
            rl_more.setSelected(true);
        }
    }
}
